
import HeadPrefab from "../../../component/HeadPrefab";
import DateUtil from "../../../engine/DateUtil";
import { EventManager } from "../../../engine/EventManager";
import { Logger } from "../../../engine/Logger";
import UIManager from "../../../engine/UIManager";
import ViewBase from "../../../engine/ViewBase";
import { JhaoProto } from "../../../lib/net/protobuf/jhaoproto";
import { ClubSocketClient } from "../../../net/ClubSocketClient";
import { ClubConfig } from "../../config/ClubConfig";
import { EventConfig } from "../../config/EventConfig";
import { ViewConfig } from "../../config/ViewConfig";
import { UserModel } from "../../model/UserModel";
import { UIHelper } from "../../util/UIHelper";
import ViewClubMember from "./ViewClubMember";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewClubMemberDetail extends ViewBase {

    private data: JhaoProto.IClub_User;
    private parent: ViewClubMember


    public onInitChild(data: JhaoProto.IClub_User, parent: ViewClubMember) {
        this.data = data;
        this.parent = parent
        this.refresh()
    }

    public refresh() {
        let head: HeadPrefab = this.getChild(this.node, "head", HeadPrefab)
        head.setHead(this.data.headimgurl)
        let txtName: cc.Label = this.getChild(this.node, "txtName", cc.Label)
        txtName.string = this.data.nickName
        let txtId: cc.Label = this.getChild(this.node, "txtId", cc.Label)
        txtId.string = this.data.userid
        let txtShenpi: cc.Label = this.getChild(this.node, "txtShenpi", cc.Label)
        if (this.data.approver) {
            txtShenpi.string = "" + this.data.approver
        } else {
            txtShenpi.string = ""
        }
        let txtJoinTime: cc.Label = this.getChild(this.node, "txtJoinTime", cc.Label)
        txtJoinTime.string = DateUtil.formatDateStr(this.data.joinTime)
        let txtScore: cc.Label = this.getChild(this.node, "txtScore", cc.Label)
        txtScore.string = this.data.score + ""
        let btnKill: cc.Node = this.findChild(this.node, "controlNode/btnKill")
        let btnCancelManager: cc.Node = this.findChild(this.node, "controlNode/btnCancelManager")
        let btnBeManager: cc.Node = this.findChild(this.node, "controlNode/btnBeManager")
        btnKill.active = false
        btnCancelManager.active = false
        btnBeManager.active = false;
        let btnScore: cc.Node = this.findChild(this.node, "controlNode/btnScore")
        btnScore.active = false
        if (UserModel.instance.nowClub.actor == ClubConfig.Club_Actor_Type.owner ||
            UserModel.instance.nowClub.actor == ClubConfig.Club_Actor_Type.administrator) {
            btnKill.active = true
            if (this.data.actor == ClubConfig.Club_Actor_Type.administrator) { //被设置对象已经是管理员
                btnCancelManager.active = true;
                btnBeManager.active = false;
            } else {
                btnCancelManager.active = false;
                btnBeManager.active = true;
            }
            btnScore.active = true
        }

        //自己 或者设置的对象是圈主
        if (this.data.userid == UserModel.instance.userId || this.data.userid == ClubConfig.Club_Actor_Type.owner) {
            btnKill.active = false
            btnCancelManager.active = false
            btnBeManager.active = false;
        }

        UIHelper.onAddEvent(btnKill, () => {
            ClubSocketClient.req_club_delete_member(UserModel.instance.nowClub.clubid, this.data.userid, () => {
                this.parent.deleteMember(this.data)
                this.closeView()
            })
        })

        UIHelper.onAddEvent(btnBeManager, () => {
            ClubSocketClient.req_club_member_actor(UserModel.instance.nowClub.clubid, this.data.userid, ClubConfig.Club_Actor_Type.administrator, () => {
                this.data.actor = ClubConfig.Club_Actor_Type.administrator;
                this.refresh()
                this.parent.refreshMember()
            })
        })
        UIHelper.onAddEvent(btnCancelManager, () => {
            ClubSocketClient.req_club_member_actor(UserModel.instance.nowClub.clubid, this.data.userid, ClubConfig.Club_Actor_Type.general, () => {
                this.data.actor = ClubConfig.Club_Actor_Type.general;
                this.refresh()
                this.parent.refreshMember()
            })
        })

        UIHelper.onAddEvent(btnScore, () => {
            UIManager.instance.showView(ViewConfig.viewConfig.ViewClubScore, null, this.data, this)
        })

    }




}
